<?php

namespace app\server\notice\controller;

use app\server\notice\model\EmsSimple;
use app\server\notice\model\Push;
use app\server\notice\model\SmsSimple;

class Index
{
    private $_params = [];

    private $_type;

    private $_flag = false;

    /**
     * 短信映射策略
     *
     * @var array
     */
    private $_smsMapStrategy = [
        'sms_1' => 'template_code_login',
        'sms_2' => 'template_code_register',
        'sms_3' => 'template_code_pwd',
        'sms_4' => 'template_new_user',
        'sms_5' => 'template_code_common',
        'sms_6' => 'template_code_re_pwd',
        'sms_7' => 'template_code_re_mobile',
    ];

    /**
     * 邮件映射策略
     *
     * @var array
     */
    private $_emsMapStrategy = [
        'ems_1' => 'login',
        'ems_2' => 'register',
        'ems_3' => 'pwd'
    ];

    /**
     * Index constructor.
     * @param $params
     */
    public function __construct($params)
    {
        if (is_scalar($params)) {
            $this->_params = json_decode($params, true);
        }

        $this->_params['tem'] = $this->_params['tem'] ?? 'nothing';

        // 短信策略
        if (key_exists($this->_params['tem'], $this->_smsMapStrategy)) {
            if (!empty($this->_params['content']) && !empty($this->_params['mobile'])) {
                $this->_flag = true;
                echo 'sms1';
                $this->_type = new SmsSimple($this->_smsMapStrategy[$this->_params['tem']]);
            }
        }
        // 邮件策略
        if (key_exists($this->_params['tem'], $this->_emsMapStrategy)) {
            if (!empty($this->_params['to']) && !empty($this->_params['content'])) {
                $this->_flag = true;
                $this->_type = new EmsSimple();
            }
        }
    }

    /**
     * 检查参数
     *
     * @return bool
     */
    public function check()
    {
        return $this->_flag;
    }

    /**
     * 发送消息
     *
     * @return mixed
     */
    public function index()
    {
        if ($this->_type instanceof SmsSimple) {
            return $this->_sms();
        }
        if ($this->_type instanceof EmsSimple) {
            return $this->_ems();
        }
        return 'Missing required parameter...';
    }

    /**
     * 发送邮件
     *
     * @return mixed
     */
    private function _ems()
    {
        $push = (new Push())->setStrategy($this->_type);
        return $push->msgSend(['to' => $this->_params['to'], 'tem' => $this->_params['tem'], 'content' => $this->_params['content']]);
    }

    /**
     * 发送短信
     *
     * @return mixed
     */
    private function _sms()
    {
        $push = (new Push())->setStrategy($this->_type);
        switch ($this->_params['tem']) {
            case 'sms_4':
                $pushParams = ['name' => $this->_params['content'][0], 'password' => $this->_params['content'][1]];
                break;
            case 'sms_6':
                $pushParams = ['password' => $this->_params['content'][0]];
                break;
            case 'sms_7':
                $pushParams = ['number' => $this->_params['content'][0], 'password' => $this->_params['content'][1]];
                break;
            default:
                $pushParams = ['code' => $this->_params['content'][0]];
        }
        return $push->msgSend(['mobile' => $this->_params['mobile'], 'params' => $pushParams]);
    }
}